package com.stefanini.pe.bbva.bp.in.converter;

import java.util.List;
import java.util.Map;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

import org.springframework.web.jsf.FacesContextUtils;

import com.stefanini.pe.bbva.bp.in.bo.iface.RebalanceoBOIface;
import com.stefanini.pe.bbva.bp.in.utils.Util;

@FacesConverter(value="nemoTecnicoAccionesConverter")
public class NemoTecnicoAccionesConverter implements Converter {
	
	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String submittedValue)
			throws ConverterException {
		RebalanceoBOIface rebalanceoBO = (RebalanceoBOIface)FacesContextUtils.getWebApplicationContext(arg0)
											.getBean("rebalanceoBO");
		List<Map> listHNemoTecnico = null;
		try {
			listHNemoTecnico = rebalanceoBO.listarAutocompleteNemotecnicoAcciones(Util.getStringF(submittedValue).trim());
		} catch (Exception e) {
			e.printStackTrace();
		}

		Map hNemoTecnico = null;
		if(listHNemoTecnico != null && listHNemoTecnico.size() > 0){
			hNemoTecnico = listHNemoTecnico.get(0);
		}
		
		return hNemoTecnico;
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object value)
			throws ConverterException {		
		if (value == null || !(value instanceof Map)) {
			return null;
		}

		return Util.getStringF(((Map)value).get("nemonico"));
	}
}